from view.com_0.content_frame_base import ContentFrameBase, init_tool_button
from boot import Union, QWidget, AvatarWidget, Qt, QLabel, QVBoxLayout, IMG_ROOT_DIR, os, FontFamily
from utils.format import amount_format
from view.widget.qq9_line import QQ9Line
from view.widget.qq9_info_widget import QQ9InfoWidget
from view.widget.qq9_label import QQ9QrcodeLabel


__all__ = ["BillContentFrame"]


class BillContentFrame(ContentFrameBase):

    @init_tool_button
    def __init__(self, parent: Union[QWidget, None] = None):
        super().__init__(parent)

        # 头像
        self.avatar_info_widget = _AvatarInfoWidget()
        self.content_frame.main_v_layout.addWidget(self.avatar_info_widget, alignment=Qt.AlignHCenter)

        # 空白
        self.blank_widget = QLabel()
        self.blank_widget.setFixedHeight(50)
        self.content_frame.main_v_layout.addWidget(self.blank_widget, alignment=Qt.AlignHCenter)

        # 水平直线
        self.line = QQ9Line()
        self.line.setFixedWidth(400)
        self.content_frame.main_v_layout.addWidget(self.line, alignment=Qt.AlignHCenter)

        # 回款单号
        self.bill_no_info_widget = QQ9InfoWidget(title="回款单号", icon_path=IMG_ROOT_DIR + "/单号.png")
        self.bill_no_info_widget.setFixedWidth(400)
        self.content_frame.main_v_layout.addWidget(self.bill_no_info_widget, alignment=Qt.AlignHCenter)

        # 回款时间
        self.bill_datetime_info_widget = QQ9InfoWidget(title="回款时间", icon_path=IMG_ROOT_DIR + "/时间.png")
        self.bill_datetime_info_widget.setFixedWidth(400)
        self.content_frame.main_v_layout.addWidget(self.bill_datetime_info_widget, alignment=Qt.AlignHCenter)

        # 回款方式
        self.bill_type_info_widget = QQ9InfoWidget(title="回款方式", icon_path=IMG_ROOT_DIR + "/类型.png")
        self.bill_type_info_widget.setFixedWidth(400)
        self.content_frame.main_v_layout.addWidget(self.bill_type_info_widget, alignment=Qt.AlignHCenter)

        # 回款备注
        self.remark_info_widget = QQ9InfoWidget(title="回款备注", icon_path=IMG_ROOT_DIR + "/备注.png")
        self.remark_info_widget.setFixedWidth(400)
        self.content_frame.main_v_layout.addWidget(self.remark_info_widget, alignment=Qt.AlignHCenter)

        # 客户合计

        self.total_amount_info_widget = QQ9InfoWidget(title="客户合计", icon_path=IMG_ROOT_DIR + "/支付.png")
        self.total_amount_info_widget.setFixedWidth(400)
        self.total_amount_info_widget.content_label.setStyleSheet(
            f"""
            font-family: "{FontFamily.wechat_sans}";
            font-size: 16px;
            color: #ED464E;
            background-color: transparent;
            """
        )
        self.content_frame.main_v_layout.addWidget(self.total_amount_info_widget, alignment=Qt.AlignHCenter)

        # 二维码
        self.qrcode_label = QQ9QrcodeLabel(100, 100)
        self.content_frame.main_v_layout.addWidget(self.qrcode_label, alignment=Qt.AlignHCenter)

    def set_info(self, icon_path, cust_name, bill_amount, bill_id, bill_datetime, bill_type, remark, all_amount):
        # 1.头像区域
        self.avatar_info_widget.set_avatar_info(icon_path=icon_path, cust_name=cust_name, bill_amount=bill_amount)
        # 2.详细信息区域
        self.bill_no_info_widget.set_info(bill_id)
        self.bill_datetime_info_widget.set_info(bill_datetime)
        self.bill_type_info_widget.set_info(bill_type)
        self.remark_info_widget.set_info(remark)
        self.total_amount_info_widget.set_info(amount_format(all_amount))


class _AvatarInfoWidget(QWidget):

    def __init__(self, parent: Union[QWidget, None] = None):
        super().__init__(parent)

        # 主垂直布局
        self.main_v_layout = QVBoxLayout()
        self.main_v_layout.setContentsMargins(0, 0, 0, 0)
        self.main_v_layout.setAlignment(Qt.AlignTop)
        self.main_v_layout.setSpacing(20)
        self.setLayout(self.main_v_layout)

        # 插入头像
        self.avatar_widget = AvatarWidget()
        self.avatar_widget.setFixedSize(70, 70)  # 明确设置头像控件尺寸
        self.avatar_widget.setContentsMargins(0, 0, 0, 0)
        self.main_v_layout.addWidget(self.avatar_widget, alignment=Qt.AlignHCenter)

        # 显示姓名
        self.cust_name_label = QLabel()
        self.cust_name_label.setAlignment(Qt.AlignCenter)
        self.cust_name_label.setStyleSheet(
            f"""
            font-family: "微软雅黑";
            font-size: 18px;
            color: #666666;
            background-color: transparent;
            """
        )
        self.main_v_layout.addWidget(self.cust_name_label, alignment=Qt.AlignHCenter)

        # 显示金额
        self.bill_amount_label = QLabel()
        self.bill_amount_label.setFixedHeight(80)
        self.bill_amount_label.setAlignment(Qt.AlignCenter)
        self.bill_amount_label.setStyleSheet(
            f"""
            font-family: "{FontFamily.wechat_sans}";
            font-size: 45px;
            color: #111111;
            background-color: transparent;
            """
        )
        self.main_v_layout.addWidget(self.bill_amount_label, alignment=Qt.AlignHCenter)

    def set_avatar_info(self, icon_path: str = "", cust_name: str = "", bill_amount: str = ""):
        self.avatar_widget.setImage(icon_path)
        self.avatar_widget.setRadius(35)
        self.avatar_widget.setAlignment(Qt.AlignCenter)
        # 2.加载姓名
        self.cust_name_label.setText(cust_name)
        # 3.加载金额
        self.bill_amount_label.setText(amount_format(bill_amount))